package exttable.timetable.handler;

import exttable.SGTTestParametrs;
import exttable.timetable.ETestType;
import exttable.timetable.TimeTableSaver;
import exttable.timetable.models.AnalysisModel;
import exttable.timetable.models.VisitModel;
import exttable.view.ViewAnalysis;
import utils.Localisation;

import javax.swing.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Apr 30, 2010
 * Time: 12:16:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class TimeTableHandler {
    private static final int ANALYSIS = 1;
    private static final int YES = 0;

    public boolean analysisCheckerValidator(Vector<VisitModel> visitModel, JTable[] analysisInArray, SGTTestParametrs ourInstance) {
        Map<ETestType, List<AnalysisModel>> mapVisitAnalysis = new HashMap<ETestType, List<AnalysisModel>>(3);

        for (JTable table : analysisInArray) {
            List<AnalysisModel> amList = new ArrayList<AnalysisModel>();
            if (table != null) {
                for (int row = 0; row < table.getModel().getRowCount(); row++) {
                    amList.add(getListOfVisits(table, visitModel, row));
                }
                mapVisitAnalysis.put(ETestType.valueOf(table.getName()), amList);
            }
        }

        Object[] options = {Localisation.getMessage("form.yes"), Localisation.getMessage("form.no")};

        int choice = JOptionPane.showOptionDialog(null
                , Localisation.getMessage("warring.datawillbeadded")
                , Localisation.getMessage("warring.waring")
                , JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
        if (choice == YES) {
            try {
                return new TimeTableSaver().saveInDB(mapVisitAnalysis, ourInstance);
            } catch (Exception e1) {
                e1.printStackTrace();
                return false;
            }
        }
        return false;
    }

    private AnalysisModel getListOfVisits(JTable table, Vector<VisitModel> visitList, int row) {
        AnalysisModel toReturn = new AnalysisModel();
        ViewAnalysis va = (ViewAnalysis) table.getModel().getValueAt(row, ANALYSIS);
//        System.out.println("ID: " + va.getAnalysisId());
//        System.out.println("KEY: " + va.getAnalysisVersionKey());
        toReturn.setAnalysis(va);
//        toReturn.setTestId(Integer.valueOf(table.getModel().getValueAt(row, ANALYSIS).toString()));
//        toReturn.setNameEn(table.getModel().getValueAt(row, VERSION).toString());
//        toReturn.setNameKz(table.getModel().getValueAt(row, VERSION).toString());
//        toReturn.setNameRu(table.getModel().getValueAt(row, VERSION).toString());

        List<VisitModel> vmList = new ArrayList<VisitModel>();
        for (int column = 2; column < table.getColumnCount(); column++) {
            VisitModel vm = new VisitModel();
            vm.setDays(visitList.get(column - 2).getDays());
            vm.setHours(visitList.get(column - 2).getHours());
            vm.setMinuets(visitList.get(column - 2).getMinuets());
            vm.setCarried(table.getModel().getValueAt(row, column) == null ? false : Boolean.valueOf(table.getModel().getValueAt(row, column).toString()));
            vmList.add(vm);
        }
        toReturn.setVisitModel(vmList);
        return toReturn;
    }

}
